package cn.sinata.wanglin.neighbor.activity;

import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DialogUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.adapter.NewCircleMessageAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.PushDao;
import cn.sinata.wanglin.model.CircleMessage;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class NewCircleMessageActivity extends RecyclerListActivity {

    private NewCircleMessageAdapter newCircleMessageAdapter;
    private ArrayList<CircleMessage> mCircleMessages = new ArrayList<>();
    private String userId;
    private int pageNo = 0;

    @Override
    protected void initView() {
        super.initView();
        setTitle("消息");
        addRightButton("清空", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.createNoticeDialog(mContext, "请注意", "确定要清空新消息记录吗？", "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clearCircleMsg();
                    }
                });
            }
        });
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        newCircleMessageAdapter = new NewCircleMessageAdapter(mCircleMessages);
        newCircleMessageAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext)
                        .go(CircleDetailActivity.class)
                        .put("id",mCircleMessages.get(position).getExternalID())
                        .put("toUser",mCircleMessages.get(position).getExternalUserID())
                        .startForResult(0);
            }
        });
        return newCircleMessageAdapter;
    }

    void getData(){
        HttpManager.circleNewMsg(userId,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<CircleMessage>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<CircleMessage> circleMessages) {
                        MyApplication.circleUnRead = 0;
                        String account = SharedPreferencesUtils.getString(Constant.SP_IM_ACCOUNT);
                        PushDao.getInstance(mContext).setCircleMessageAllRead(account);
                        if (pageNo == 0) {
                            mCircleMessages.clear();
                        }
                        if (circleMessages.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mCircleMessages.addAll(circleMessages);
                        newCircleMessageAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }

    void clearCircleMsg(){
        showDialog();
        HttpManager.clearCircleMsg(userId).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                mCircleMessages.clear();
                newCircleMessageAdapter.notifyDataSetChanged();
            }
        });
    }
}
